package com.lc.pianostudent.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.lc.pianostudent.R;
import com.lc.pianostudent.bean.PianoRecordBean;
import com.zcx.helper.glide.GlideLoader;
import com.zcx.helper.scale.ScaleScreenHelperFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/6/25.
 */

public class XingzuoAdapter extends RecyclerView.Adapter<XingzuoAdapter.ViewHolder>{


    private Context context;

    private List<String> list = new ArrayList<>();
    private String type;
    private int width;

    public XingzuoAdapter(Context context, List<String> list ,String type ,int width) {
        this.context = context;
        this.list = list;
        this.type = type;
        this.width = width;
    }

    public interface OnItemClickListener {

        void onItemClick(View view, int position);

    }

    private XingzuoAdapter.OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(XingzuoAdapter.OnItemClickListener listener) {

        this.onItemClickListener = listener;

    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_gridview, parent, false);

        ScaleScreenHelperFactory.getInstance().loadViewGroup((ViewGroup) view);

        XingzuoAdapter.ViewHolder viewHolder = new XingzuoAdapter.ViewHolder(view);

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
       if(type.equals("4")){
           holder.grid_icon.getLayoutParams().width = (int) (width/3.1);
           holder.grid_icon.getLayoutParams().height = (int) (width/1.96);
       }else if (type.equals("6")){
//           holder.grid_icon.setScaleY(1.2f);
//           holder.grid_icon.setScaleX(1.2f);
           holder.grid_icon.getLayoutParams().width = (int) (width/3.1);
           holder.grid_icon.getLayoutParams().height = (int) (width/2.94);
       } else if (type.equals("9")){
           holder.grid_icon.getLayoutParams().width = (int) (width/4.65);
           holder.grid_icon.getLayoutParams().height = (int) (width/2.94);
        }else if (type.equals("12")){
           holder.grid_icon.getLayoutParams().width = (int) (width/4.65);
           holder.grid_icon.getLayoutParams().height = (int) (width/3.92);
       }else if (type.equals("16")){
           holder.grid_icon.getLayoutParams().width = (int) (width/6.21);
           holder.grid_icon.getLayoutParams().height = (int) (width/3.92);
       }else if (type.equals("20")){
           holder.grid_icon.getLayoutParams().width = (int) (width/6.21);
           holder.grid_icon.getLayoutParams().height = (int) (width/4.91);
       }else if (type.equals("25")){
            holder.grid_icon.getLayoutParams().width = (int) (width/7.76);
            holder.grid_icon.getLayoutParams().height = (int) (width/4.91);
       }else if (type.equals("30")){
           holder.grid_icon.getLayoutParams().width = (int) (width/7.76);
           holder.grid_icon.getLayoutParams().height = (int) (width/5.88);
       }else if (type.equals("36")){
           holder.grid_icon.getLayoutParams().width = (int) (width/9.31);
           holder.grid_icon.getLayoutParams().height = (int) (width/5.88);
       }else if (type.equals("42")){
           holder.grid_icon.getLayoutParams().width = (int) (width/9.31);
           holder.grid_icon.getLayoutParams().height = (int) (width/6.86);
       }else if (type.equals("49")){
           holder.grid_icon.getLayoutParams().width = (int) (width/10.68);
           holder.grid_icon.getLayoutParams().height = (int) (width/6.86);
       }else if (type.equals("56")){
           holder.grid_icon.getLayoutParams().width = (int) (width/10.68);
           holder.grid_icon.getLayoutParams().height = (int) (width/7.84);
       }
        GlideLoader.getInstance().get(list.get(position),holder.grid_icon);

        setUpItemEvent(holder,position);

    }

    //为RecyclerView添加点击事件
    protected void setUpItemEvent(final XingzuoAdapter.ViewHolder holder, final  int position) {

        if (onItemClickListener != null) {

            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    //  int LayoutPosition = holder.getLayoutPosition();

                    onItemClickListener.onItemClick(holder.itemView, position);



                }

            });

        }

    }
    @Override
    public int getItemCount() {
        return list.size();
    }


    //初始化组件
    public static class ViewHolder extends RecyclerView.ViewHolder {

        public ImageView grid_icon;


        public ViewHolder(View itemView) {
            super(itemView);

            grid_icon = (ImageView) itemView.findViewById(R.id.grid_icon);


        }
    }
}
